package com.dt.dubai.mall.controller;

import com.dt.dubai.mall.entity.MallCommodityBrandEntity;
import com.dt.dubai.mall.controller.innate.MallCommodityBrandInnateAbs;
import com.dt.dubai.core.comm.*;
import com.dt.dubai.core.utile.DateTools;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Date;

/*
* 商品品牌信息表
*/

import java.util.List;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import java.lang.reflect.Type;
import com.dt.dubai.core.exception.PageErrorMesg;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.Map;

@Controller
@RequestMapping(value = "/mall/mallCommodityBrand")
public class MallCommodityBrandController extends MallCommodityBrandInnateAbs {

    /*innateServiceStart*/
    @Override
    protected void userSavEntity(MallCommodityBrandEntity inEntity, final Long userId) {
        inEntity.setDelSign(false);
    }

    @Override
    protected void userModEntity(MallCommodityBrandEntity inEntity, final Long userId) {
    }

    @Override
    protected void userDelByKey(List<Long> recordIds, final Long userId) {
    }

    @Override
    protected void userQueByKey(Long recordId, Map<String, Object> map, final Long userId) {
        if (null != map && map.size() > 0) {
            map.put("createTxStamp", DateTools.dateFormat((Date) map.get("createTxStamp"), DateTools.DATE_FORMAT_YMD));
            map.remove("delSign");
            map.remove("lastupTxStamp");
        }
    }

    @Override
    protected void userQuePageList(PageParameterData queryCondition, List<PageCondition> joinCondition, List<Map<String, Object>> resultMap, final Long userId) {
        for (Map<String, Object> map : resultMap) {
            map.put("createTxStamp", DateTools.dateFormat((Date) map.get("createTxStamp"), DateTools.DATE_FORMAT_YMD));
            Short commodityType = null == map.get("commodityType") ? -1 :  (Short) map.get("commodityType");
            switch (commodityType) {
                case 1:
                    map.put("commodityType", commodityType);
                    map.put("commodityTypeStr", "电商");
                    break;
                case 2:
                    map.put("commodityType", commodityType);
                    map.put("commodityTypeStr", "检测");
                    break;
                case 3:
                    map.put("commodityType", commodityType);
                    map.put("commodityTypeStr", "计量");
                    break;
                default:
                    map.put("commodityType", -1);
                    map.put("commodityTypeStr", "未知");
            }
            Integer brandType = null == map.get("brandType") ? -1 :  (Integer) map.get("brandType");
            switch (brandType) {
                case 1:
                    map.put("brandType", brandType);
                    map.put("brandTypeStr", "区域品牌");
                    break;
                case 2:
                    map.put("brandType", brandType);
                    map.put("brandTypeStr", "产地品牌");
                    break;
                default:
                    map.put("brandType", -1);
                    map.put("brandTypeStr", "未知");
            }
            map.remove("delSign");
            map.remove("lastupTxStamp");
        }
    }

    /*innateServiceEnd*/
    /*以上内容请误删除或是移动，系统升级使用，用户代码请写入下方*/

    @RequestMapping(value = "/brandNameList", method = RequestMethod.POST)
    @ResponseBody
    public PageData<String> findBrandNameList(@RequestParam("data")  String data,final Long userId) {
        // 品牌名列表 
        try {
            if (null == data) {
                SqlUtils.clearSqlCode(data);
                SqlUtils.clearWebCode(data);
             }
            Type type = new TypeReference<Map<String, Object>>() {
            }.getType();
            Map<String, Long> map = JSON.parseObject(data, type);
            Map<String, Object> result = this.mallCommodityBrandSer.findBrandNameList(map.get("commodityType"), userId);
            return new PageData().success(result, i18nUtile.getMessage(PageErrorMesg.E0.name()));
        } catch (RuntimeException e) {
            e.printStackTrace();
            return PageData.error(PageErrorMesg.E50.name(), i18nUtile.getMessage(PageErrorMesg.E50.name()));
        }
    }


}
